package com.xunshangwang.pinhaohuo.ui.activities;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.app.MainApplication;
import com.xunshangwang.pinhaohuo.http.HttpTool;



/**
 * Created by xzp on 15/9/17.
 */
public abstract class BaseActivity extends AppCompatActivity{
    private Toolbar mToolbar;
    private TextView mToolbarTitleText;
   // Drawable mBackDrawable;
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        mToolbar= (Toolbar) this.findViewById(R.id.toolbar);
        if(mToolbar!=null)
        {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayUseLogoEnabled(false);
//            mBackDrawable=ContextCompat.getDrawable(this, android.support.v7.appcompat.R.drawable.abc_ic_ab_back_mtrl_am_alpha);
//            getSupportActionBar().setHomeAsUpIndicator(mBackDrawable);
//            setHomeAsUpColor(ContextCompat.getColor(this,R.color.dark));
            mToolbarTitleText= (TextView) mToolbar.findViewById(R.id.toolbar_title_text);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            if(mToolbarTitleText!=null)
                mToolbarTitleText.setText(this.getTitle());
        }



    }

    @Override
    protected void onResume() {
        super.onResume();
       // MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
      //  MobclickAgent.onPause(this);
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public TextView getTitleText() {
        return mToolbarTitleText;
    }

//    protected void setHomeAsUpColor(int color)
//    {
//        mBackDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
//    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();
        switch (id)
        {
            case android.R.id.home:
                onHomeAsUpClick();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    protected void onHomeAsUpClick(){
        this.finish();
    }
    public MainApplication getMainApplication()
    {
        return (MainApplication) getApplication();
    }

    @Override
    protected void onDestroy() {
        HttpTool.cancelRequests(this);
        super.onDestroy();
    }
}
